package com.baizhi;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baizhi.service.UserService;
import com.baizhi.service.impl.UserServiceImpl;
import com.baizhi.util.MailUtil;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class UserAction {
    private String emai;
    private String pwd;
    private User user;
    private String email;
    private int state;
    private String emailName;

    public String getEmailName() {
        return emailName;
    }

    public void setEmailName(String emailName) {
        this.emailName = emailName;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getYanzheng() {
        return yanzheng;
    }

    public void setYanzheng(String yanzheng) {
        this.yanzheng = yanzheng;
    }

    private String yanzheng;
    private UserService us=new UserServiceImpl();

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }


    public String getEmai() {
        return emai;
    }

    public void setEmai(String emai) {
        this.emai = emai;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public String login(){
        boolean login = us.login(emai, pwd);
        int id = us.selectId(emai);
        HttpSession session = ServletActionContext.getRequest().getSession();
        session.setAttribute("uid",id);
        session.setAttribute("emai",emai);
        session.setAttribute("pwd",pwd);
        System.out.println(emai+pwd);
        if (login){
            return "sucess";
        }else{
            return "defat";
        }
    }
    public void yazheng()  {
        //产生验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(116, 34, 4, 20);
        //把图片交给response的相应给浏览器
        HttpServletResponse response = ServletActionContext.getResponse();
        //获取到response的响应流
        ServletOutputStream stream = null;
        try {
            stream = response.getOutputStream();
            //将图片放入到response响应的流中
            captcha.write(stream);
            //获取到生成验证码的值
            String code = captcha.getCode();
            //放入session
            ServletActionContext.getRequest().getSession().setAttribute("code",code);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                stream.close();//关流;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public String register(){
        String code = (String)ServletActionContext.getRequest().getSession().getAttribute("code");
        String contextPath = ServletActionContext.getRequest().getContextPath();
        String email = user.getEmail();
        System.out.println(email);
        ServletActionContext.getRequest().getSession().setAttribute("email",email);
        //判断验证码是否一样  equalsIgnoreCase方法是不区分大小写的判断
        if (code.equalsIgnoreCase(yanzheng)){
                us.register(user,pwd);
            MailUtil.sendSimpleMail(user.getEmail(),"这是一封激活邮件","<a href='http://192.168.45.30:9090"+contextPath+"/User/User_jihuo?email="+email+"&state=1'>点击激活</a>");
                return "registerSucess";
        }else{
            return "registerdefat";
        }
    }
    public String jihuo(){
        String emails =(String)ServletActionContext.getRequest().getSession().getAttribute("email");
        System.out.println(email);
        System.out.println(state);
        us.updateState(email,state);
        user = us.userMessage(email);
        return "update";
    }
    public String s(){//清空session缓存
        ServletActionContext.getRequest().getSession().setAttribute("emai","");
        ServletActionContext.getRequest().getSession().setAttribute("pwd","");
        return "ss";
    }
    public void ma(){
     UserService a=new UserServiceImpl();
        User user = a.userMessage(emailName);
        HttpServletResponse response = ServletActionContext.getResponse();//获取response对象
        PrintWriter writer=null;
        try {
             writer = response.getWriter();
             if (emailName.equals(user.getEmail())){
                 writer.print("true");
             }else{
                 writer.print("false");
             }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            writer.flush();
        }
    }
}
